package extend.db.entity.field;

import extend.db.entity.EntityReflectUtil;

import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Map;

/**
 * 实体属性注解配置
 * <B>说明：</B>
 * <pre>
 *  略
 * </pre>
 * <B>示例：</B>
 * <pre>
 *  略
 * </pre>
 * <B>日志：</B>
 * <pre>
 *  略
 * </pre>
 * <B>注意事项：</B>
 * <pre>
 *  略
 * </pre>
 */
public class EntityField
{

    public final static String FIELD_NAME = "fieldName";
    public final static String COLUMN_NAME = "columnName";
    public final static String FORMAT_NAME = "format";
    public final static String VALUE_NAME = "value";


    /**
     * 字段类型
     *<B>说明：</B>
     *<pre>
     *  略
     *</pre>
     */
    protected static Map<String,Class<EntityField>> fieldTypes = new HashMap<>();

    /**
     * 实体类属性名称
     *<B>说明：</B>
     *<pre>
     *  略
     *</pre>
     */
    protected String fieldName;

    /**
     * 字段名
     *<B>说明：</B>
     *<pre>
     *  略
     *</pre>
     */
    protected String columnName;



    public static EntityField make(Map<String,Object> attrs)
    {
        Class<EntityField> entityField = (Class<EntityField>)attrs.get(FORMAT_NAME);

        try {
            Constructor constructor = entityField.getDeclaredConstructor(Map.class);
            return (EntityField)constructor.newInstance(new Object[]{attrs});
        } catch (Exception ex) {
            return new EntityField(attrs);
        }
    }

    /**
     * 是否主键字段
     *<B>说明：</B>
     *<pre>
     *  略
     *</pre>
     */
    protected boolean pk = false;

    /**
     * 如果是主键,是否自增
     *<B>说明：</B>
     *<pre>
     *  略
     *</pre>
     */
    protected boolean auto = false;

    public EntityField()
    {
    }

    public EntityField(Map<String,Object> attrs)
    {
        EntityReflectUtil.setAttrs(this,attrs);
    }

    public void setAttrs(Map<String,Object> attrs)
    {
        EntityReflectUtil.setAttrs(this,attrs);
    }

    public String getFieldName()
    {
        return this.fieldName;
    }

    public String getColumnName()
    {
        return this.columnName;
    }

    /**
     * 是否主键
     *<B>说明：</B>
     *<pre>
     *  略
     *</pre>
     */
    public boolean isPk()
    {
        return this.pk;
    }

    /**
     * 属性简单描述
     *<B>说明：</B>
     *<pre>
     *  略
     *</pre>
     */
    public boolean auto()
    {
        if (this.pk == true && this.auto == true) {
            return true;
        } else {
            return false;
        }
    }

    public boolean isNull()
    {
        return false;
    }

    public Object toColumnValue(Object value)
    {
        return value;
    }

    public Object toFieldValue(Object value)
    {
        return value;
    }

}
